/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 *
 * @author aers
 */
public class Soquete {

    private String ip;
    private int porta;
    private Socket destino;
    private ObjectOutputStream outToServer;
    private ObjectInputStream inFromServer;

    public Soquete(String ip, int porta) {
        this.ip = ip;
        this.porta = porta;
        try {
            destino = new Socket(ip,porta);
            outToServer = new ObjectOutputStream(destino.getOutputStream());
            inFromServer = new ObjectInputStream(destino.getInputStream());
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void enviarPacote(Object pacote) {
        try {
            outToServer.writeObject(pacote);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public Object receberPacote() {
        Object objeto = null;
        try {
            objeto = inFromServer.readObject();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return objeto;
    }

    public void close() {
        try {
            outToServer.close();
            inFromServer.close();
            destino.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
     

}
